/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-04-11 14:35:53
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-06-15 17:50:12
 */

'use strict';
(function(win, doc, loc, his) {
    var fmpVersion = {};
    /**
     * { function_description }
     *
     * @method     init
     * @param      {<type>}  html    { 需要加载的页面 }
     */
    fmpVersion.init = function(html) {
        if (!html) {
            console.error('html is not exist!');
            return;
        }

        fmpVersion.ajax({
            url: 'http://walle.clientgear.com/sysmonitor/version',
            type: 'POST',
            dataType: 'json',
            success: function(response, xml) {
                var v = fmpVersion.toJson(response);
                console.info(v.version)
                var htmlPath = v.version + '/' + html + window.location.hash;
                var mainIframe = document.getElementById('main-iframe');
                if (mainIframe) {
                    mainIframe.setAttribute("src", htmlPath);
                } else {
                    mainIframe = document.createElement("iframe");
                    mainIframe.setAttribute("id", "main-iframe");
                    mainIframe.setAttribute("width", "100%");
                    mainIframe.setAttribute("height", "100%");
                    mainIframe.setAttribute("frameborder", "0");
                    mainIframe.setAttribute("src", htmlPath);
                    document.body.appendChild(mainIframe);
                }
            },
            fail: function(status) {
                console.info(status)
            }
        });
    }

    /**
     * [testInit 测试]
     * @param  {[type]} html [description]
     * @return {[type]}      [description]
     */
    fmpVersion.testInit = function(html) {
        if (!html) {
            console.error('html is not exist!');
            return;
        }

        var htmlPath = '../html/' + html + window.location.hash;
        var mainIframe = document.getElementById('main-iframe');
        if (mainIframe) {
            mainIframe.setAttribute("src", htmlPath);
        } else {
            mainIframe = document.createElement("iframe");
            mainIframe.setAttribute("id", "main-iframe");
            mainIframe.setAttribute("width", "100%");
            mainIframe.setAttribute("height", "100%");
            mainIframe.setAttribute("frameborder", "0");
            mainIframe.setAttribute("src", htmlPath);
            document.body.appendChild(mainIframe);
        }
    }

    fmpVersion.ajax = function(options) {
        options = options || {};
        options.type = (options.type || "GET").toUpperCase();
        options.dataType = options.dataType || "json";
        var params = fmpVersion.formatParams(options.data);

        //创建 - 非IE6 - 第一步
        if (window.XMLHttpRequest) {
            var xhr = new XMLHttpRequest();
        } else { //IE6及其以下版本浏览器
            var xhr = new ActiveXObject('Microsoft.XMLHTTP');
        }

        //接收 - 第三步
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                var status = xhr.status;
                if (status >= 200 && status < 300) {
                    options.success && options.success(xhr.responseText, xhr.responseXML);
                } else {
                    options.fail && options.fail(status);
                }
            }
        }

        //连接 和 发送 - 第二步
        if (options.type == "GET") {
            xhr.open("GET", options.url + "?" + params, true);
            xhr.send(null);
        } else if (options.type == "POST") {
            xhr.open("POST", options.url, true);
            //设置表单提交时的内容类型
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(params);
        }
    }

    //格式化参数
    fmpVersion.formatParams = function(data) {
        var arr = [];
        for (var name in data) {
            arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
        }
        arr.push(("v=" + Math.random()).replace("."));
        return arr.join("&");
    }



    fmpVersion.toJson = function(str) {
        return (new Function("return " + str))();
    }
    var winVersion = win.XXXversion;
    if (!winVersion) return console.error('mkq error! Pixel code is not installed correctly on this page');
    winVersion.invokeFunc = function(event, param1, param2) {
        if (event) {
            event = event.toLowerCase();
            if ('init' == event) {
                fmpVersion.init(param1, param2);
            } else if ('testinit' == event) {
                fmpVersion.testInit(param1, param2);
            }
        }
    }
    var winParams = winVersion.qu.slice();
    for (var x = 0, y = winParams.length; x < y; x++) {
        winVersion.invokeFunc.apply(winVersion, winParams[x]);
    }
})(window, document, location, history);
